//
//  CoHUD.swift
//  jocool
//
//  Created by zhuxietong on 2016/11/7.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import Foundation
import MBProgressHUD
public enum EeHudStatus{
    case loading(msg:String?)
    case success(msg:String?)
    case notice(msg:String?)
    case error(err:String?)
    case dissmis
}

public class EeHUD:NSObject
{
    public static let share = EeHUD()
    public override init() {
        super.init()
    }
    
    public static var _do:(_:EeHudStatus,_ at:UIView?)->Void = {
        status,view  in

        if let a_view = view{
        
//            var hud:MBProgressHUD? = MBProgressHUD(view: a_view)
//            if hud == nil{
            let hud:MBProgressHUD? = MBProgressHUD.showAdded(to: a_view, animated: true)
//            }
            hud?.backgroundView.style = .solidColor
            hud?.backgroundView.color = .color(light: "2222", dark: "2222")
            hud?.label.font = UIFont.systemFont(ofSize: 15)
            
            switch status {
            case .success(msg: let msg),.notice(msg: let msg),.error(err: let msg),.loading(msg: let msg):
                if let  _msg = msg{
                    hud?.isSquare = false
//                    hud?.minSize = [80,80]
                    hud?.label.text = _msg
                }else{
//                    hud?.minSize = [80,80]
                    hud?.isSquare = true
                }
            case .dissmis:
                break
            }
            
            
            switch status{
            case .success(msg: _):
                let img = UIImage(named: "hud_success_mark")
                hud?.customView = UIImageView(image: img)
                hud?.mode = .customView
                hud?.hide(animated: true, afterDelay: 2.5)
            case .error(err: _):
                let img = UIImage(named: "hud_failed_mark")
                hud?.customView = UIImageView(image: img)
                hud?.mode = .customView
                hud?.hide(animated: true, afterDelay: 3)
            case .loading(msg: _):
                hud?.mode = .indeterminate
            case .dissmis:
                MBProgressHUD.hide(for: a_view, animated: false)
                a_view.isUserInteractionEnabled = true
            case .notice(msg: let msg):
                hud?.__show(msg, detail: nil, image: nil, minSize: nil)
                //                hud?.__loading(msg, detail: nil, minSize: nil)
                //                hud?.hide(animated: true, afterDelay: 1.5)
            }
            
            
        }
        
    }
    
    public static var _defaultBackgroundView:UIView?{
        get{
            var back:UIView? = UIApplication.shared.keyWindow
            if back == nil{
                back = UIApplication.shared.keyWindow?.rootViewController?.view
            }
            return back
        }
    }
    
    public var background:UIView? = nil
    
    public init(backView:UIView? = nil) {
        self.background = backView
    }
    
    public func `do`(_ status:EeHudStatus) {
        
        var v = background
        if v == nil{
            v = EeHUD._defaultBackgroundView
        }
        
        
        EeHUD._do(status,v)
        print("+++_OO\(EeHUD._do)")
    }
    
    public static func quickDo(_ status:EeHudStatus) {
        EeHUD.share.do(status)
//        print("_)))ISIISISIIS==\(EeHUD.share)=")

    }
    
    @objc public static func ocDo(tag:String,msg:String?){
        switch tag {
        case "loading":
            quickDo(.loading(msg: msg))
        case "notice":
            quickDo(.notice(msg: msg))
        case "dissmis":
            quickDo(.dissmis)
        case "error":
            quickDo(.error(err: msg))
        default:
            quickDo(.dissmis)
        }
    }
}




public class CoHud {
    public static var rootBack:UIView?{
        get{
            var back:UIView? = UIApplication.shared.keyWindow
            if back == nil{
                back = UIApplication.shared.keyWindow?.rootViewController?.view
            }
            return back
        }
    }
    
    public static var _loading:(UIView?)->Void = {
        _ in
    }
    
    public static var _dismiss:(UIView?)->Void = {
        _ in
    }
    
    public class func loading()
    {
        CoHud._loading(nil)
    }
    
    public class func loading(_ at:UIView?=CoHud.rootBack) {
        CoHud._loading(at)
        
        at?.isUserInteractionEnabled = false
    }
    
    public class func loading(at controller:UIViewController?) {
        if let ctr = controller
        {
            CoHud._loading(ctr.view)
            ctr.view.isUserInteractionEnabled = false
        }
    }

    
    public class func dismiss() {
        CoHud._dismiss(nil)
    }
    
    public class func dismiss(_ at:UIView?=CoHud.rootBack) {
        CoHud._dismiss(at)
        at?.isUserInteractionEnabled = true

    }
    
    
    public class func dismiss(at controller:UIViewController?) {
        if let ctr = controller
        {
            CoHud._dismiss(ctr.view)
            ctr.view.isUserInteractionEnabled = true
        }
    }
    
}

